﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GMUtilitiesDotNet.ViewModels;
using ThouShaltNotBrickLevelEditor.Code.Managers;
using ThouShaltNotBrickLevelEditor.Code.GeneralObjects;
using System.ComponentModel;
using GMUtilitiesDotNet.Helpers;
using ThouShaltNotBrick.Models;
using GMUtilitiesDotNet.Commands;
using System.Windows.Input;

namespace ThouShaltNotBrickLevelEditor.ViewModels
{
    public sealed class LevelViewModel : ViewModelBase
    {
        #region Properties
        #region BrickStatesTypesImages
        public static readonly PropertyChangedEventArgs BrickStatesTypesImagesEventArgs = ObservableHelper.CreateArgs<LevelViewModel>(x => x.BrickStatesTypesImages);

        /// <summary>
        /// BrickStatesTypesImages property.
        /// </summary>
        public BrickStatesTypesImages BrickStatesTypesImages
        {
            get { return mLevelManager.BrickStatesTypesImages; }
        }
        #endregion

        #region LevelData
        public static readonly PropertyChangedEventArgs LevelDataEventArgs = ObservableHelper.CreateArgs<LevelViewModel>(x => x.LevelData);

        /// <summary>
        /// LevelData property.
        /// </summary>
        public LevelData LevelData
        {
            get { return mLevelManager.LevelData; }
        }
        #endregion

        private LevelManager mLevelManager;
        #endregion

        #region Commands

        #region AddBrickCommand
        private RelayCommand mAddBrickCommand;
        public ICommand AddBrickCommand
        {
            get
            {
                if (mAddBrickCommand == null)
                {
                    mAddBrickCommand = new RelayCommand(
                        param => mLevelManager.AddBrick((BrickPosition)param),
                        param => mLevelManager.CanAddBrick());
                }
                return mAddBrickCommand;
            }
        }
        #endregion

        #region DeleteBrickCommand
        private RelayCommand mDeleteBrickCommand;
        public ICommand DeleteBrickCommand
        {
            get
            {
                if (mDeleteBrickCommand == null)
                {
                    mDeleteBrickCommand = new RelayCommand(
                        param => mLevelManager.DeleteBrick((BrickPosition)param),
                        param => mLevelManager.CanDeleteBrick());
                }
                return mDeleteBrickCommand;
            }
        }
        #endregion
        #endregion

        #region Constructors
        public LevelViewModel(LevelEditorManager pLevelEditorManager)
        {
            mLevelManager = pLevelEditorManager.LevelManager;
            mLevelManager.BrickStatesTypesImagesChanged += BrickStatesTypesImagesChangedHandler;
            mLevelManager.LevelDataChanged += LevelDataChangedHandler;
        }
        #endregion

        #region Overriden Methods
        protected override void OnDisposeManaged()
        {
            mLevelManager.BrickStatesTypesImagesChanged -= BrickStatesTypesImagesChangedHandler;
            mLevelManager.LevelDataChanged -= LevelDataChangedHandler;

            base.OnDisposeManaged();
        }
        #endregion

        #region Event Handlers
        private void BrickStatesTypesImagesChangedHandler(object sender, EventArgs e)
        {
            RaisePropertyChangedEvent(BrickStatesTypesImagesEventArgs);
        }

        private void LevelDataChangedHandler(object sender, EventArgs e)
        {
            RaisePropertyChangedEvent(LevelDataEventArgs);
        }
        #endregion
    }
}